var fila_actual = -1;
var gridRegImagenes;
var gridSeparador;
var miLayout;
var toolbar;
var win1;
var win2;

function mostrarVentanaDocumentos(titulo){
    fila_actual = -1;
    dhxLayout.dhxWins = new dhtmlXWindows();
    dhxLayout.dhxWins.setImagePath("images/dhtmlx/dhxwins_dhx_black/");
    
    var win = dhxLayout.dhxWins.createWindow('ventana', 28, 58, 900, 550);
            dhxLayout.dhxWins.setSkin("dhx_black");
            dhxLayout.dhxWins.window('ventana').centerOnScreen();
            dhxLayout.dhxWins.window('ventana').setModal(true);
            dhxLayout.dhxWins.window('ventana').maximize();
    win.setText(titulo);
    //miLayout = win.attachLayout("2U", "dhx_black");
//alert(exelink);
    //win.attachURL(exelink, true);
        //dhxWins.window('ventana').setDimension(900, 700);
    //layout1 = win.attachLayout("2U", "dhx_black");

    /*************************/

    var formName = "forma";
    var vForm=document.getElementById(formName);
    var vSendPost="";
    //var vLoad='<div class=\"LoadingOut\"><b class=\"rtop\"><b class=\"r1\"></b> <b class=\"r2\"></b> <b class=\"r3\"></b> <b class=\"r4\"></b></b><div class=\"LoadingIn\"><br>Cargando Informaci&oacute;n...<br><img src=\"images/bigWaiting.gif\"><br></div><b class=\"rbottom\"><b class=\"r4\"></b> <b class=\"r3\"></b> <b class=\"r2\"></b> <b class=\"r1\"></b></b></div>';

    for (var i=0;i<vForm.elements.length;i++){
        var vType=vForm.elements[i].type;
        var vName=vForm.elements[i].name;
        var vNameAnterior = "";
        var vValue = "";

        if(vName != vNameAnterior)
            agregar = true;

        switch (vType){
            case "text":
                vValue=vForm.elements[i].value;
                if (i>0 && i<(vForm.elements.length))
                    vSendPost=vSendPost+"&";
                vSendPost += vName+"="+vValue;
                break;
            case "password":
                vValue=vForm.elements[i].value;
                if (i>0 && i<(vForm.elements.length))
                    vSendPost=vSendPost+"&";
                vSendPost += vName+"="+vValue;
                break;
            case "hidden":
                vValue=encodeURIComponent(vForm.elements[i].value);
                if (i>0 && i<(vForm.elements.length))
                    vSendPost=vSendPost+"&";
                vSendPost += vName+"="+vValue;
                break;
            case "checkbox":
                vValue=0;
                if (vForm.elements[i].checked==true) vValue=vForm.elements[i].value;
                if (i>0 && i<(vForm.elements.length))
                    vSendPost=vSendPost+"&";
                vSendPost += vName+"="+vValue;
                break;
            case "radio":
                //vValue=0;
                //if (vForm.elements[i].checked==true) var vValue=vForm.elements[i].value;
                //var vValue=vForm.elements[i].value;
                if(vName != vNameAnterior){
                    if (vForm.elements[i].checked){
                        vValue=vForm.elements[i].value;
                        vNameAnterior = vName;
                        if (i>0 && i<(vForm.elements.length))
                            vSendPost=vSendPost+"&";
                        vSendPost += vName+"="+vValue;
                    }
                }
                break;
            case "select-one":
                vValue=vForm.elements[i].value;
                if (i>0 && i<(vForm.elements.length))
                    vSendPost=vSendPost+"&";
                vSendPost += vName+"="+vValue;
                break;
            case "textarea":
                vValue = vForm.elements[i].value;
                if (i>0 && i<(vForm.elements.length))
                    vSendPost=vSendPost+"&";
                vSendPost += vName+"="+vValue;
                break;
        }
    }

    //var titulo = document.getElementById("titulo").value;
    titulo = document.getElementById("titulo").value;
    miLayout = win.attachLayout("2U", "dhx_black");
    miLayout.setEffect("resize", true);
    miLayout.cells("a").setText("Resultados Obtenidos: "+titulo);
    miLayout.cells("b").setText("Imagen");
    miLayout.setAutoSize("a;b","a;b");

    toolbar = miLayout.cells("a").attachToolbar();
    toolbar.setIconsPath("images/dhtmlx/imgs/");
    toolbar.loadXML("toolbar.xml?etc="+ new Date().getTime());
    toolbar.attachEvent("onClick", function(id){
        if(id == "separador"){
            //toolbar.disableItem(id);
            miLayout.dhxWins = new dhtmlXWindows();
            miLayout.dhxWins.setImagePath("images/dhtmlx/dhxwins_dhx_black/");

            win1 = miLayout.dhxWins.createWindow('ventana_separador', 28, 58, 400, 400);
            miLayout.dhxWins.setSkin("dhx_black");
            miLayout.dhxWins.window('ventana_separador').centerOnScreen();
            //miLayout.dhxWins.window('ventana_separador').center();
            miLayout.dhxWins.window('ventana_separador').setModal(true);
            win1.setText("Separadores Virtuales");

            var id_tdl_datos = gridRegImagenes.getSelectedId();
            gridSeparador = win1.attachGrid();
            gridSeparador.setSkin("sbdark");
            gridSeparador.setImagePath("images/dhtmlx/");
            gridSeparador.init();
            gridSeparador.loadXML("sbn_AdministrarDocumentos.php?accion=separador&id_tdl_datos="+id_tdl_datos);
            gridSeparador.attachEvent("onRowSelect", consultarImagenesSeparador);
        }else if(id == "info"){
            //toolbar.disableItem(id);
            miLayout.dhxWins = new dhtmlXWindows();
            miLayout.dhxWins.setImagePath("images/dhtmlx/dhxwins_dhx_black/");

            win2 = miLayout.dhxWins.createWindow('ventana_info', 28, 58, 200, 200);
            miLayout.dhxWins.setSkin("dhx_black");
            miLayout.dhxWins.window('ventana_info').centerOnScreen();
            //miLayout.dhxWins.window('ventana_info').center();
            miLayout.dhxWins.window('ventana_info').setModal(true);
            win2.setText("Información del Documento");
        }else if(id == "salir")
            win.close();
    });

    gridRegImagenes = miLayout.cells("a").attachGrid();
    gridRegImagenes.setImagePath("images/dhtmlx/");
    gridRegImagenes.setSkin("sbdark");
    gridRegImagenes.init();
    gridRegImagenes.enableSmartRendering(true, 50);
    gridRegImagenes.loadXML("sbn_AdministrarDocumentos.php?accion=consultar&"+vSendPost);
    gridRegImagenes.attachEvent("onRowSelect", consultarImagenes);

    miLayout.cells("b").collapse();
    /***************************/
}

function consultarImagenes(id){
    if(fila_actual != id){
        fila_actual = id;
        glbCurrentPhoto = 1;
        indice_rotacion = -1;
        var total_paginas = gridRegImagenes.cells(id, 0).getValue();

        if(total_paginas > 0){
            miLayout.cells("b").expand();
            miLayout.cells("b").attachURL('sbn_AdministrarDocumentos.php?accion=consultarImagenes&id_tdl_datos='+id+'&total_paginas='+total_paginas, true);
        }else document.getElementById("imagen").innerHTML = "";
    }
}

function consultarImagenesSeparador(id){
    indice_rotacion = 0;
    var pagina_actual = gridSeparador.cells(id, 0).getValue();
    var total_paginas = gridRegImagenes.cells(gridRegImagenes.getSelectedId(), 0).getValue();

    if(total_paginas > 0){
        miLayout.cells("b").expand();
        miLayout.cells("b").attachURL('sbn_AdministrarDocumentos.php?accion=consultarImagenes&id_tdl_datos='+id+'&pagina_actual='+pagina_actual+'&total_paginas='+total_paginas, true);
    }else document.getElementById("imagen").innerHTML = "";
}

function cargarUsuarios(select){
    var id_guser = select.options[select.selectedIndex].value;
    if(id_guser > 0)
        cargarContenido("combo_usuarios","sbn_ModuloSeguridad.php?accion=cargarUsuarios&id_guser="+id_guser);
    else document.getElementById("combo_usuarios").innerHTML = "";
    document.getElementById("tree_estructuras").innerHTML = "";
    document.getElementById("tree_estruct_conf").innerHTML = "";
}
var tree_estructuras;
var tree_estruct_conf;

function cargarTrees(select){
    var id_user = select.options[select.selectedIndex].value;
    if(id_user > 0){
        tree_estructuras = new dhtmlXTreeObject("tree_estructuras","100%","100%",0);
        tree_estructuras.setImagePath("images/dhtmlx/csh_vista/");
        tree_estructuras.enableCheckBoxes(1);
        tree_estructuras.enableThreeStateCheckboxes(true);
        tree_estructuras.loadXML("sbn_ModuloSeguridad.php?accion=treeEstructuras&id_user="+id_user);

        tree_estruct_conf = new dhtmlXTreeObject("tree_estruct_conf","100%","100%",0);
        tree_estruct_conf.setImagePath("images/dhtmlx/csh_vista/");
        tree_estruct_conf.loadXML("sbn_ModuloSeguridad.php?accion=treeEstructurasConf&id_user="+id_user);
    }
}

function guardarRestricciones(){
    var select = document.getElementById("combo_usuarios");
    var id_user = select.options[select.selectedIndex].value;
    if(id_user > 0)
        cargarContenido('Contenido', 'sbn_ModuloSeguridad.php?accion=guardarRestricciones&id_user='+id_user+'&ids='+tree_estructuras.getAllCheckedBranches());
}

function validarCampo(obCampo, tipo){
    
}

function cerrarVentana(){
    win1.close();
    win2.close();
}